// pagesProperty/pages/event/task/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    taskList: [],
    netWork: true,
    state: 0,
    hasData: false,
    eventType: 0,
    pageIndex: 1,
    pageSize: 10,
    currentTab: 0,
    isShowPop: false,
    dateType: '',
    beginDate: '',
    endDate: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.state){
      this.data.state = parseInt(options.state);
      this.setData({
        currentTab: parseInt(options.state)
      })
    }
    if(options.type){
      this.data.eventType = parseInt(options.type);
    }

    this.onHttpInit();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  handleChange({detail}){    
    console.log('detail',detail)
    this.data.pageIndex = 1;
    this.setData({
      taskList : [],
      currentTab: detail.index
    })
    this.data.state = detail.index;
    this.onHttpInit();
  },
  searchData(e){
    var type = e.currentTarget.dataset.type;
    this.data.dateType = type;
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      taskList:[],
    })
    this.onHttpInit();
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/plan/task/list', {
      state: that.data.state,
      dateType: that.data.dateType,
      beginDate: that.data.beginDate,
      endDate: that.data.endDate,
      eventType:that.data.eventType,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("获取任务列表", res)
      if (res.data.code == 0) {
        var tasks = res.data.data.tasks;
        if(tasks.length > 0){
          that.data.pageIndex += 1;
          for (var index in tasks) {
            that.data.taskList.splice(that.data.taskList.length, 0, tasks[index]);
          }
          that.setData({
            hasData: true,
            taskList: that.data.taskList
          })
        }
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    })
  },
  goDetail(e){
    var item  =e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../detail/index?id=' + item.id,
    })
  },
  chooseDate(e){
    this.setData({
      isShowPop: !this.data.isShowPop
    })
  },
  onCloseCalendar(e){
    console.log('onCloseCalendar', e)
    this.setData({
      isShowPop: false,
    });
  },
  onConfirmCalendar(event){
    console.log('onConfirmCalendar',event)
    const [start, end] = event.detail;
    var startDate = common_Js.formatTimeSimple(start);
    var endDate = common_Js.formatTimeSimple(end);
    console.log('startDate',startDate)
    console.log('endDate',endDate)

    this.data.dateType = "range";
    this.data.beginDate = startDate;
    this.data.endDate = endDate;
    this.setData({
      isShowPop: false,
    });
    
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      taskList:[],
    })
    this.onHttpInit();
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      taskList:[],
    })
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpInit();
  },
})